#******************************************************************
#
# Copyright 2014 Intel Mobile Communications GmbH All Rights Reserved.
#
#-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
#-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=

Import('env')

target_os = env.get('TARGET_OS')
transport = env.get('TARGET_TRANSPORT')
secured = env.get('SECURED')
routing = env.get('ROUTING')
with_mq = env.get('WITH_MQ')
OIC_LIB = 'oic'
root_dir = env.get('ROOT_DIR')
build_dir = env.get('BUILD_DIR')
sample_dir = build_dir

print("MQ flag is %s" % with_mq)

env.AppendUnique(CPPFLAGS=[
    '-std=c++0x', '-fPIC', '-D__TIZEN__', '-DWITH_POSIX', '-Wall',
    '-DSLP_SDK_LOG', '-g', '-D_GNU_SOURCE', '-DTIZEN_DEBUG_ENABLE', '-DTB_LOG',
])
env.ParseConfig("pkg-config --cflags --libs dlog com.oic.ri capi-network-connection")
env.ParseConfig("pkg-config --cflags --libs gobject-2.0 glib-2.0")

if routing == 'GW':
    env.AppendUnique(CPPDEFINES=['ROUTING_GATEWAY'])
elif routing == 'EP':
    env.AppendUnique(CPPDEFINES=['ROUTING_EP'])

env.Append(LIBS=[
    'm',
    'pthread',
    'rt',
    'dl',
    'stdc++',
    'gobject-2.0',
    'gio-2.0',
    'glib-2.0',
    'capi-network-connection',
    'dlog',
    'capi-network-bluetooth',
    'connectivity_abstraction',
    'coap',
    'octbstack',
    'ocsrm',
    'c_common',
])

if secured == '1':
    env.PrependUnique(CPPPATH=[root_dir + '/external/inc/'])
    env.AppendUnique(CPPDEFINES=['__WITH_DTLS__'])
    env.Append(LIBS=['mbedtls', 'mbedx509', 'mbedcrypto'])

if 'ALL' in transport:
    env.AppendUnique(CPPDEFINES=[
        'IP_ADAPTER', 'EDR_ADAPTER', 'LE_ADAPTER', 'BT_ADAPTER_TEST',
        'BLE_ADAPTER_TEST'
    ])
    print("CA Transport is ALL")
else:
    if 'BT' in transport:
        env.AppendUnique(CPPDEFINES=['EDR_ADAPTER', 'BT_ADAPTER_TEST'])
        print("CA Transport is BT")
    else:
        env.AppendUnique(CPPDEFINES=['NO_EDR_ADAPTER'])

    if 'BLE' in transport:
        env.AppendUnique(CPPDEFINES=['LE_ADAPTER', 'BLE_ADAPTER_TEST'])
        print("CA Transport is BLE")
    else:
        env.AppendUnique(CPPDEFINES=['NO_LE_ADAPTER'])

    if 'IP' in transport:
        env.AppendUnique(CPPDEFINES=['IP_ADAPTER'])
        print("CA Transport is IP")
    else:
        env.AppendUnique(CPPDEFINES=['NO_IP_ADAPTER'])

    if env.get('WITH_TCP'):
        env.AppendUnique(CPPDEFINES=['TCP_ADAPTER', 'WITH_TCP'])
    else:
        env.AppendUnique(CPPDEFINES=['NO_TCP_ADAPTER'])

if 'SUB' in with_mq:
    env.AppendUnique(CPPDEFINES=['MQ_SUBSCRIBER', 'WITH_MQ'])
    print("MQ SUB support")

if 'PUB' in with_mq:
    env.AppendUnique(CPPDEFINES=['MQ_PUBLISHER', 'WITH_MQ'])
    print("MQ PUB support")

if 'BROKER' in with_mq:
    env.AppendUnique(CPPDEFINES=['MQ_BROKER', 'WITH_MQ'])
    print("MQ Broker support")

#ri_sample_src = [sample_dir + '/occlient.cpp']

#print " ri sample src %s" % ri_sample_src

#env.Program('ri_sample', [ri_sample_src,])

sample_comm = sample_dir + 'common.cpp'
env.Program('occlient', [sample_dir + 'occlient.cpp', sample_comm])
env.Program('ocserver', [sample_dir + 'ocserver.cpp', sample_comm])
env.Program('ocrouting', [sample_dir + 'ocrouting.cpp', sample_comm])
